<template>
  <div class="article-page">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item>面经后台</el-breadcrumb-item>
      <el-breadcrumb-item>面经管理</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card shadow="never" border="false">
      <template #header>
        <div class="header">
              <span>共 {{total}}条记录</span>
          <el-button
            icon="el-icon-plus"
            size="small"
            type="primary"
            round
            @click="showDrawer('add')"
            >
            添加面经
          </el-button>
        </div>
      </template>
      <el-table :data="rows" stripe style="width: 100%">
  <el-table-column prop="stem" label="标题" width="400">
  </el-table-column>
  <el-table-column prop="creator" label="作者" >
  </el-table-column>
  <el-table-column prop="likeCount" label="点赞" > </el-table-column>
  <el-table-column prop="views" label="浏览数" > </el-table-column>

  <el-table-column prop="createdAt" label="更新时间" width="200"> </el-table-column>
  <el-table-column label="操作">
      <template slot-scope="scope">
        <div class="actions">
          <i class="el-icon-view" @click="showDrawer('view',scope.row.id)"></i>
          <i class="el-icon-edit" @click="showDrawer('edit',scope.row.id)"></i>
          <i class="el-icon-delete" @click="delData(scope.row.id)"></i>
        </div>
      </template>
    </el-table-column>
</el-table>
<el-pagination
  background
  layout="prev, pager, next"
  :page-count="pageTotal"
  @current-change="changeCurrent($event)"
  >
</el-pagination>
<el-drawer
  :title="title"
  :visible.sync="drawer"
  :direction="direction"
  :before-close="handleClose"
  ref="drawer"
  size="50%">
    <div v-if="status==='view'" class="article-preview">
      <h5>{{ form.stem }}</h5>
      <div v-html="form.content"></div>
    </div>
    <el-form ref="formName"  :rules="rules" :model="form" label-width="80px" v-else>
        <el-form-item label="标题" prop="stem">
          <el-input placeholder="输入面经标题" v-model="form.stem"></el-input>
        </el-form-item>
        <el-form-item label="内容" prop="content">
          <quill-editor v-model="form.content"></quill-editor>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submit">确认</el-button>
          <el-button>取消</el-button>
        </el-form-item>
      </el-form>
</el-drawer>
    </el-card>
  </div>
</template>

<script>
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'

import { quillEditor } from 'vue-quill-editor'

import { addArticle, delArticle, getArticleList, getArticleDetail, updataArticle } from '@/api/article'

export default {
  name: 'article-page',
  components: {
    quillEditor
  },
  data () {
    return {
      p: {
        current: 1,
        pageSize: 5

      },
      form: {
        stem: '',
        content: ''
      },
      rows: [],
      total: 0,
      pageTotal: 10,
      drawer: false,
      direction: 'rtl',
      status: 'add',
      rules: {
        stem: { required: true, message: '标题不能为空', trigger: 'blur' },
        content: { required: true, message: '内容不能为空', trigger: 'blur' }
      }

    }
  },
  async created () {
    this.showArticle()
  },
  methods: {
    async showArticle () {
      const res = await getArticleList(this.p)
      // console.log(res)
      this.rows = res.data.rows
      this.total = res.data.total
      this.pageTotal = res.data.pageTotal
      console.log(this.pageTotal)
    },
    changeCurrent (e) {
      this.p.current = e
      this.showArticle()
    },
    async delData (id) {
      await delArticle({ id: id })
      // const index = this.rows.findIndex(item => item.id === id)
      // this.rows.splice(index, 1)
      this.showArticle()
      this.$message.success('删除成功')
    },
    async  showDrawer (status, id) {
      this.drawer = true
      this.status = status
      if (status === 'add') {
        this.$refs.formName.resetFields()
      } else {
        const res = await getArticleDetail(id)
        // console.log(res)
        this.form = {
          ...res.data
        }
      }
    },
    submit () {
      // console.dir(this.form)
      this.$refs.formName.validate(async (valid) => {
        if (!valid) return
        if (this.status === 'add') {
          this.form = { stem: this.form.stem, content: this.form.content }
          await addArticle(this.form)

          // console.log(res)
          this.handleClose()
          this.showArticle()
          this.$message.success('添加成功')
        }
        if (this.status === 'edit') {
          const { id, stem, content } = this.form
          const res = await updataArticle({ id, stem, content })
          console.log(res)
          this.handleClose()
          this.showArticle()
          this.$message.success('修改成功')
        }
      })
    },
    handleClose () {
      this.$refs.formName.resetFields()

      this.drawer = false
    }

  },
  computed: {
    title () {
      if (this.status === 'add') {
        return '添加面经'
      } else if (this.status === 'view') {
        return '面经预览'
      } else {
        return '修改面经'
      }
    }
  }

}
</script>

<style lang="scss" scoped>
.el-card {
  margin-top: 25px;
  .header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding-right: 16px;
  }
  .actions {
    font-size: 18px;
    display: flex;
    justify-content: space-around;
    color: #666;
    > i:hover {
      color: rgba(114, 124, 245, 1);
      cursor: pointer;
    }
  }
}
.el-pagination {
  margin-top: 20px;
  text-align: center;
}
.el-breadcrumb {
  margin-top: 10px;
}
.el-form {
  padding-right: 40px;
}
.quill-editor {
  ::v-deep .ql-editor {
    height: 300px;
  }
}
.el-rate {
  padding: 10px 0;
}
.article-preview {
  padding: 0 40px 40px 40px;
  > h5 {
    font-size: 20px;
    color: #666;
    border-bottom: 1px dashed #ccc;
    padding-bottom: 30px;
    margin: 0 0 20px 0;
  }
}
</style>
